import time

from selenium import webdriver
from selenium.common import InvalidArgumentException, WebDriverException, NoSuchElementException, TimeoutException
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from common.functions import open_browser


class Base:
    def __init__(self, browser):
        self.driver = browser

    def get(self, url):  # 输入的网址
        try:
            self.driver.get(url)
        except(InvalidArgumentException, WebDriverException):
            print('输入的地址不正确')

    def window_max(self):  # 窗口最大化
        self.driver.maximize_window()

    def windou__size(self, chang, kuan):  # 窗口分辨率
        self.driver.set_window_size(chang, kuan)

    def back(self):  # 浏览器后退
        self.driver.back()

    def forward(self):  # 浏览器前进
        self.driver.forward()

    def refresh(self):  # 刷新浏览器
        self.driver.refresh()

    def title(self):  # 获取页面标题
        self.driver.title()

    def close(self):  # 关闭当前页面
        self.driver.title()

    # todo 切换frame
    def switch_frame(self, locator):
        ele = self.find_element(locator)
        if ele is not None:
            self.driver.switch_to.frame(ele)
        else:
            print('元素无效')

    # 定位
    def find_element(self, locator, timeout=5):  # locator = (By.xxx, value)
        # 定位元素
        try:
            return WebDriverWait(self.driver, timeout).until(EC.presence_of_element_located(locator))
        except TimeoutException:
            print('未获取到元素')

    def click(self, locator):  # 鼠标左键
        element = self.find_element(locator)
        if element is not None:
            element.click()
        else:
            print("元素定位失败")

    def context_click(self, locator):  # 鼠标右键
        ac = ActionChains(self.driver)
        element = self.find_element(locator)
        if element is not None:
            ac.context_click()
        else:
            print('定位失败')

    def get_element_text(self, locator):  # 定位文本框
        element = self.find_element(locator)
        if element:
            return element.text

    # todo 根据下标选取下拉菜单
    def select_by_index(self, locator, num):
        ele = self.find_element(locator)
        select = Select(ele)
        if ele is not None:
            select.select_by_index(num)
        else:
            print('元素无效')
        # todo 根据value选取下拉菜单

    def select_by_value(self, locator, value):
        ele = self.find_element(locator)
        select = Select(ele)
        if ele is not None:
            select.select_by_value(value)
        else:
            print('元素无效')

        # todo 根据文本选取下拉菜单

    def select_by_visible_text(self, locator, text):
        ele = self.find_element(locator)
        select = Select(ele)
        if ele is not None:
            select.select_by_visible_text(text)
        else:
            print('元素无效')

        # todo 滚动条滑到底部

    def scroll_under(self):
        self.driver.execute_script('window.scrollTo(0,document.body.scrollHeight)')

        # todo 滚动条滑到顶部

    def scroll_top(self):
        self.driver.driver.execute_script('window.scrollTo(0,0)')

    def double_click(self, locator):  # 鼠标双击
        ac = ActionChains(self.driver)
        element = self.find_element(locator)
        if element is not None:
            ac.double_click()
        else:
            print('定位失败')

    # def drag_and_drop(self, locator,locator1):  # 鼠标拖动
    #     ac = ActionChains(self.driver)
    #     element = self.find_element(locator)
    #     if element is not None:
    #         ac.context_click()
    #     else:
    #         print('定位失败')

    def send_keys(self, locator, value):  # 输入类容
        element = self.find_element(locator)
        if element is not None:
            element.send_keys(value)
        else:
            print('元素定位失败')

    def get_attribute(self, locator, value):  # 查看定位目标的源代码
        self.value = self.find_element(locator)
        element = self.find_element(locator)
        if element is not None:
            print(element.get_attribute(value))
        else:
            print('元素定位失败')

    def implicitly_wait(self, seconds=10): #隐视等待
        self.driver.implicitly_wait(seconds)
    def alert(self):
        self.driver.switch_to.alert

    def quit(self, seconds=2):  # 关闭浏览器`
        time.sleep(seconds)
        self.driver.quit()


if __name__ == '__main__':
    browser = open_browser()
    tast = Base(browser)

    tast.get('https://www.baidu.com')

    # # tast.windou__size(800, 800)
    # # tast.window_max()
    # # tast.back()
    # # time.sleep(2)
    # # tast.forward()
    # # time.sleep(2)
    # # tast.forward()
    # # time.sleep(2)
    # id1 = [By.ID, 'kw']
    # tast.find_element(id1,3)
    # # time.sleep(3)
    # # tast.context_click(id1)
    # time.sleep(3)
    # tast.get_attribute(id1,'outerHTML')
    tast.quit()

